var app=require('ajax.js');
var config=require('config.js');

var fileUpload={
    //选择图片并上传
    chooseImage:function(url,formData,count,succCallback,errorCallback){
        fileUpload.wxChooseImage(url,formData,count,succCallback,errorCallback);
    },
    //预览图片
    previewImage:function(address,imageObject){
        fileUpload.wxPreviewImage(address,imageObject);
    },
    chooseImages:function(url,formData,count){
        return new Promise((resolve,reject)=>{
            wx.chooseImage({
                count:count,//最多可以选择的图片张数，默认9
                sizeType:['original','compressed'],
                sourceType:['album','camera'],
                success: function(res) {
                    //返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
                    const tempFilePaths=res.tempFilePaths;
                    Promise.all(tempFilePaths.map((img)=>{
                        return new Promise((reso,rej)=>{
                            fileUpload.wxUpload(url,img,formData,function(file){
                                reso(file);
                            },function(err){
                                rej(err);
                            })
                        })
                    })).then((res)=>{
                        resolve(res);
                    }).catch((err)=>{
                        reject(err);
                    })
                },
                fail:function(e){
                    reject(e);
                }
            })
        })
    },
    //选择图片
    wxChooseImage:function(url,formData,count,succCallback,errorCallback){
        let that=this;
        wx.chooseImage({
            count:count,
            sizeType:['original','compressed'],
            sourceType:['album','camera'],
            success: function(res) {
                var tempFilePaths=res.tempFilePaths[0];
                fileUpload.wxUpload(url,tempFilePaths,formData,succCallback,errorCallback);
            },
            fail:function(){

            }
        })
    },
    //上传图片
    wxUpload:function(url,tempFilePaths,formData,succCallback,errorCallback){
        wx.uploadFile({
            url: config.ServerUrl+url,
            filePath: tempFilePaths,
            name: 'file',
            formData: formData,//和服务器约定的token, 一般也可以放在header中
            success:function(res){
                if(config.debug){
                    console.log(res);
                }
                succCallback(JSON.parse(res.data).data,res.statusCode)
            },
            fial:function(e){
                console.log(e);
                wx.showModal({
                    title: '提示',
                    content: '上传失败',
                })
            }
        })
    },
    //预览图片
    wxPreviewImage:function(address,imageObject){
        wx.previewImage({
            urls: imageObject,
            current:address,
            success:function(){

            },
            fail:function(){
                wx.showToast({
                    title: '预览图片失败',
                    icon:'fail',
                    duration:2000
                })
            }
        })
    }
}
module.exports=fileUpload;